<?php
namespace Home\Controller;
use Think\Controller;
class SalesController extends CommonController {
    public function index(){
        if($_POST['keyword']){
            switch ($_POST['type']) {
                case "week":
                    $lastday=date('Y-m-d',strtotime(date("Y-m-d",strtotime($_POST['keyword']))." Sunday"));
                    $firstday=date('Y-m-d',strtotime("$lastday -6 days"));
                    break;
                case "month":
                    $firstday = date('Y-m-01',strtotime(date("Y-m-d",strtotime($_POST['keyword']))));
                    $lastday = date('Y-m-d',strtotime("$firstday +1 month -1 day"));
                    break;
                case "year":
                    $lastday=date('Y-m-d',strtotime(date("Y-m-d",strtotime($_POST['keyword']))." Sunday"));
                    $firstday=date('Y-m-d',strtotime("$lastday -6 days"));
                    break;
                default:
                    $lastday=date('Y-m-d',strtotime(date("Y-m-d",strtotime($_POST['keyword']))." Sunday"));
                    $firstday=date('Y-m-d',strtotime("$lastday -6 days"));
                    break;
            }
        }else{
            switch ($_POST['type']) {
                case "week":
                    $lastday=date('Y-m-d',strtotime(date("Y-m-d")." Sunday"));
                    $firstday=date('Y-m-d',strtotime("$lastday -6 days"));
                    break;
                case "month":
                    $firstday = date('Y-m-01',strtotime(date("Y-m-d")));
                    $lastday = date('Y-m-d',strtotime("$firstday +1 month -1 day"));
                    break;
                case "year":
                    $lastday=date('Y-m-d',strtotime(date("Y-m-d")." Sunday"));
                    $firstday=date('Y-m-d',strtotime("$lastday -6 days"));
                    break;
                default:
                    $lastday=date('Y-m-d',strtotime(date("Y-m-d")." Sunday"));
                    $firstday=date('Y-m-d',strtotime("$lastday -6 days"));
                    break;
            }
        }

        $cond['pay_time'] = array('between',array(strtotime($firstday.' 00:00:00'),strtotime($lastday.' 23:59:59')));
        $res = D('order')->getSalesCount($cond);
        $arr_r[] = array();
        $str = getAllDate($firstday,$lastday);
        $array = explode(',',$str);
        $data = '';
        if($res){
            foreach ($res as $k => $v){
                if(in_array($v['date'],$array)){
                    $data = $v['sum'] . ',';
                }else{
                    $data = 0 . ',';
                }
            }
            $data = substr($data,0,strlen($data)-1);
        }

        $this->assign('type',$_POST['type']);
        $this->assign('xAxis',$str);
        $this->assign('series',$data);
        $this->display();
    }
}

function getAllDate($startDate,$endDate){
    $date = '';
    $startTime = strtotime($startDate);
    $endTime = strtotime($endDate);

    $middle = $startTime;

    while ( $middle <= $endTime ) {
        $date .= date('Y-m-d',$middle) . ',';
        $middle = strtotime('+1 day',$middle);
    }
    $date = substr($date,0,strlen($date)-1);
    return $date;
}